{% for backend in haproxy_backend %}
backend {{ backend.name }}
{% if backend.description is defined %}
  description {{ backend.description }}
{% endif %}

{% if backend.bind_process is defined %}
  bind-process {{ backend.bind_process | join(' ') }}
{% endif %}

  mode {{ backend.mode }}

  balance {{ backend.balance }}

{% if backend.source is defined %}
  source {{ backend.source }}
{% endif %}

{% for option in backend.option | default([])%}
  option {{ option }}
{% endfor %}

{% for option in backend.no_option | default([])%}
  no option {{ option }}
{% endfor %}

{% if backend.http_check is defined %}
  http-check {{ backend.http_check }}
{% endif %}

{% if backend.cookie is defined %}
  cookie {{ backend.cookie }}
{% endif %}

{% for acl in backend.acl | default([]) %}
  acl {{ acl.string }}
{% endfor %}

{% for stick in backend.stick | default([]) %}
  stick-table {{ stick.table }}
  stick on {{ stick.stick_on }}
{% endfor %}

{% for option in backend.no_option | default([]) %}
  no option {{ option }}
{% endfor %}

{% if backend.no_log | default(false) == true %}
  no log
{% endif %}

{% for tcp_check in backend.tcp_check | default([]) %}
  tcp-check {{ tcp_check }}
{% endfor %}

{% for timeout in backend.timeout | default([]) %}
  timeout {{ timeout.type }} {{ timeout.timeout }}
{% endfor %}

{% for action in ['reqadd', 'rspadd'] %}
{% for params in backend[action] | default([]) %}
  {{ action }} {{ params.string }}{% if params.cond is defined %} {{ params.cond }}{% endif %}

{% endfor %}
{% endfor %}

{% for action in ['reqdel', 'reqidel', 'rspdel', 'rspidel'] %}
{% for params in backend[action] | default([]) %}
  {{ action }} {{ params.search }}{% if params.cond is defined %} {{ params.cond }}{% endif %}

{% endfor %}
{% endfor %}

{% for action in ['reqrep', 'reqirep', 'rsprep', 'rspirep'] %}
{% for params in backend[action] | default([]) %}
  {{ action }} {{ params.search }} {{ params.string }}{% if params.cond is defined %} {{ params.cond }}{% endif %}

{% endfor %}
{% endfor %}

{% if backend.stats is defined %}
{% if backend.stats.enable is defined and backend.stats.enable | bool == true %}
  stats enable
  stats uri {{ backend.stats.uri | default('/') }}
{% if backend.stats.refresh is defined %}
  stats refresh {{ backend.stats.refresh }}
{% endif %}
{% if backend.stats.admin is defined %}
  stats admin {{ backend.stats.admin }}
{% endif %}
{% for option in backend.stats.options | default([]) %}
  stats {{ option }}
{% endfor %}
{% for auth in backend.stats.auth | default([]) %}
  stats auth {{ auth.user }}:{{ auth.passwd }}

{% endfor %}
{% endif %}
{% endif %}

{% for http_request in backend.http_request | default([]) %}
  http-request {{ http_request.action }}{% if http_request.param is defined %} {{ http_request.param }}{% endif %}{% if http_request.cond is defined %} {{ http_request.cond }}{% endif %}

{% endfor %}

{% for tcp_request_inspect_delay in backend.tcp_request_inspect_delay | default([]) %}
  tcp-request inspect-delay {{ tcp_request_inspect_delay.timeout }}

{% endfor %}

{% for tcp_request_content in backend.tcp_request_content | default([]) %}
  tcp-request content {{ tcp_request_content.action }}{% if tcp_request_content.cond is defined %} {{ tcp_request_content.cond }}{% endif %}

{% endfor %}

{% for http_response in backend.http_response | default([]) %}
  http-response {{ http_response.action }}{% if http_response.param is defined %} {{ http_response.param }}{% endif %}{% if http_response.cond is defined %} {{ http_response.cond }}{% endif %}

{% endfor %}

{% for compression in backend.compression | default([]) %}
  compression {{ compression.name }} {{ compression.value }}
{% endfor %}

{% if backend.default_server_params | default([]) %}
  default-server {% for param in backend.default_server_params | default([]) %} {{ param }}{% endfor %}
{% endif %}

{% for server in backend.server | default([]) %}
  server {{ server.name }} {{ server.listen }}{% for param in server.param | default([]) %} {{ param }}{% endfor %}

{% endfor %}
{% if backend.server_template is defined %}
  server-template {{ backend.server_template.name }} {{ backend.server_template.num}} {{ backend.server_template.fqdn }}{% if backend.server_template.port is defined %}:{{ backend.server_template.port }}{% endif %} {% for param in backend.server_template.param | default([]) %} {{ param }}{% endfor %}
{% endif %}

{% if backend.retry_on is defined %}
  retry-on {% for r in backend.retry_on %}{{ r }} {% endfor %}
{% endif %}

{% if backend.retries is defined %}
  retries {{ backend.retries }}
{% endif %}
{% for errorfile in backend.errorfile | default([]) %}
  errorfile {{ errorfile.code }} {{ errorfile.file }}
{% endfor %}

{% for line in backend.raw_options | default([]) %}
  {{ line }}
{% endfor %}
{% endfor %}
